import axios from "axios";
import Qs from "qs";
import store from "@/store"
import loading from "@/utils/loading"
import router from "../router";
import storageCenter from "./storageCenter";

const Axios = axios.create({
  baseURL: "http://120.27.68.231:8099",
  timeout: 10000,
  responseType: "json",
  withCredentials: false,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  }
});

Axios.interceptors.request.use(
  config => {
    loading.showLoading()
    if (config.method == "post") {
      let postData = config.data;
      let nArgs = {};
      for (let key in postData) {
        nArgs[key] = postData[key];
      }
      // if (store.state.userInfo.token && store.state.userInfo.token != undefined && store.state.userInfo.token != '') {
      //   nArgs['token'] = store.state.userInfo.token;
      // }
      let d = Qs.stringify(nArgs, { arrayFormat: "repeat" });
      config.data = d;
    }
    return config;
  },
  error => {
    loading.hideLoading()
    return Promise.reject(error);
  }
);

// 返回状态判断(添加响应拦截器)
Axios.interceptors.response.use(
  res => {
    loading.hideLoading()
    // let data = res.data
    // if (data.result == '0') {
    //   store.state.userInfo.token = ''
    //   storageCenter.storageData(() => {});
    //   router.replace("/login")
    // }
    return res;
  },
  error => {
    loading.hideLoading()
    return Promise.reject(error);
  }
);

export default Axios;
